
//
//  HHPHotViewModel.m
//  HHPLiveVideo
//
//  Created by 韩红坡 on 2017/4/19.
//  Copyright © 2017年 韩红坡. All rights reserved.
//

#import "HHPHotViewModel.h"
#import "HHPMediatorManager+Living.h"

@interface HHPHotViewModel ()

@property (nonatomic, assign) NSInteger pageNO;

@property (nonatomic, strong) HHPHomeService *service;

/** 点击cell执行的block，selectionIsChanged为YES表示此cell之前未被选中 */
@property (nonatomic, copy) void (^cellSelectedBlock)(HHPTableViewCellModel *cellModel, BOOL selectionIsChanged);

@end

@implementation HHPHotViewModel
#pragma mark - public methods
- (void)fetchNewRoomLiveDataWithType:(RoomType)type completionBlock:(void (^)(BOOL))completionBlock
{
    HHPHTTPRequest *request = [self.service fetchRoomLiveDataWithPageNO:@"1" type:type];
    
    [request startWithSuccess:^(HHPHTTPRequest *request) {
        self.hotLiveArray = request.data;
        
        [self.hotLiveArray enumerateObjectsUsingBlock:^(HHPLiveModel * _Nonnull liveModel, NSUInteger idx, BOOL * _Nonnull stop) {
            liveModel.didSelectCellBlock = self.cellSelectedBlock;
        }];
        
        completionBlock(YES);
    } error:^(HHPHTTPRequest *request) {
        completionBlock(NO);
    }];
}

- (void)fetchMoreRoomLiveDataWithType:(RoomType)type successBlock:(void (^)(NSUInteger))successBlock errorBlock:(void (^)(NSError *))errorBlock
{
    if (self.hotLiveArray.count % 20 == 0) {
        self.pageNO = self.hotLiveArray.count / 20 + 1;
    }else{
        self.pageNO = self.hotLiveArray.count / 20 + 2;
    }
    
    HHPHTTPRequest *request = [self.service fetchRoomLiveDataWithPageNO:[NSString stringWithFormat:@"%ld", self.pageNO] type:type];
    
    [request startWithSuccess:^(HHPHTTPRequest *request) {
        NSMutableArray *array = request.data;
        
        [array enumerateObjectsUsingBlock:^(HHPLiveModel * _Nonnull liveModel, NSUInteger idx, BOOL * _Nonnull stop) {
            liveModel.didSelectCellBlock = self.cellSelectedBlock;
        }];
        
        [self.hotLiveArray addObjectsFromArray:array];
        
        successBlock(array.count);
    } error:^(HHPHTTPRequest *request) {
        errorBlock(request.error);
    }];
}

#pragma mark - response events
- (void (^)(HHPTableViewCellModel *, BOOL))cellSelectedBlock
{
    if (!_cellSelectedBlock) {
        @weakify(self)
        _cellSelectedBlock = ^(HHPTableViewCellModel *cellModel, BOOL selectionIsChanged) {
            @strongify(self)
            HHPLiveModel *model = (HHPLiveModel *)cellModel;
            NSUInteger index = [self.hotLiveArray indexOfObject:model];
            
            [[HHPMediatorManager sharedInstance] mediator_presentLivingViewControllerWithLiveArray:self.hotLiveArray currentLiveIndex:index];
        };
    }
    
    return _cellSelectedBlock;
}

#pragma mark - getter and setter
- (HHPHomeService *)service
{
    if (!_service) {
        _service = [[HHPHomeService alloc] init];
    }
    
    return _service;
}

- (NSMutableArray<HHPLiveModel *> *)hotLiveArray{
    if (!_hotLiveArray) {
        _hotLiveArray = [NSMutableArray array];
    }
    
    return _hotLiveArray;
}

@end
